﻿var app = angular.module('app', ['angular.jquery']);
var item = null;
var flag = false;
var url = Editor.Url;

app.directive('jqdatepicker', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {
            element.datepicker({
                dateFormat: 'dd-mm-yy',
                onSelect: function (date) {
                    scope.nv.nGAYSINH = date;
                    scope.$apply();
                }
            });
        }
    };
});

app.directive('demongay', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {
            element.datepicker({
                dateFormat: 'dd-mm-yy',
                onSelect: function (date) {
                    scope.nv.nGAYSINH = date;
                    scope.$apply();
                }
            });
        }
    };
});

app.controller('DemoCtrl', [
    '$scope', '$http',
    'AppDialogService',
function ($scope, $http, AppDialogService) {
    'use strict';
    //var url = Editor.Url + "TD/GetAll";
    $http.get(url + "/nhanvien/GetAll")
     .success(function (response) {
         $scope.nhanviens = response;
         $scope.totalItems = $scope.nhanviens.length;
         $scope.itemsPerPage = 5
         $scope.currentPage = 1;
         $scope.maxSize = 5;
         $scope.bigTotalItems = 175;
         $scope.bigCurrentPage = 1;
         $scope.pageCount = function () {
             return Math.ceil($scope.nhanviens.length / $scope.itemsPerPage);
         };

         $scope.$watch('currentPage + itemsPerPage', function () {
             var begin = (($scope.currentPage - 1) * $scope.itemsPerPage),
                 end = begin + $scope.itemsPerPage;

             $scope.filterednhanviens = $scope.nhanviens.slice(begin, end);
         });
     });

    $http.get(url + "/TD/GetAll")
    .success(function (response) {
        $scope.trinhdos = response;        
    });
    $http.get(url + "/vu/GetVuDetails")
    .success(function (response) {
        $scope.vus = response;
    });

    $scope.loadData = function () {
        $http.get(url + "/nhanvien/GetAll")
                .success(function (response) {
                    $scope.nhanviens = response;
                });
    }

    $scope.delete = function (id) {
        var a = confirm("Bạn có thực sự muốn xóa dòng này?");
        if (a === false)
            return;
        $http.delete(url + "/api/NhanVienAPI/" + id).success(function (data, status, headers) {
            $scope.loadData();
        });
    }

    $scope.appDialog = function (_flag, _item) {
        item = _item;
        flag = _flag;
        AppDialogService.openDialog({
            testValue: 'application'
        }).then(function (result) {
            if (result.ok) {
                if (flag) {
                    console.dir(result.obj);
                    $http.put(url + "/api/NhanVienAPI/" + result.obj.iD, result.obj).success(function (data, status, headers) {
                        if (status === 200) {
                            alert("cập nhập thành công");
                        }
                    });
                }
                else {
                    $http.post(Editor.Url + "/api/NhanVienAPI", result.obj).success(function (data, status, headers) {
                        $scope.loadData();
                    });
                }
            }
        });

    };

}]);
app.directive('datepicker', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {
            element.datepicker();
            element.bind('blur keyup change', function(){
                var model = attrs.ngModel;
                if (model.indexOf(".") > -1) scope[model.replace(/\.[^.]*/, "")][model.replace(/[^.]*\./, "")] = element.val();
                else scope[model] = element.val();
            });
        }
    };
});
/*
app.directive("datepicker", function () {
    return {
        restrict: "A",
        require: "ngModel",
        link: function (scope, elem, attrs, ngModelCtrl) {
            var updateModel = function (dateText) {
                scope.$apply(function () {
                    ngModelCtrl.$setViewValue(dateText);
                });
            };
            var options = {
                dateFormat: "dd/mm/yy",
                onSelect: function (dateText) {
                    updateModel(dateText);
                }
            };
            elem.datepicker(options);
        }
    }
});
*/
app.controller('DialogCtrl', [
    '$scope',
    'AppDialogService',
function ($scope, AppDialogService) {
    'use strict';

    $scope.nv = null;

    $scope.onOpen = function () {        
        $scope.nv = item;
        $scope.value = new Date();
        if (item != null){
            try {
                var datetime = new Date($scope.nv.nGAYSINH);                
                $scope.value = datetime;// new Date(datetime.getYear(), datetime.getMonth(), datetime.getDate());
            }
            catch (ex) {            
                console.dir(ex);
            }
        } 
    };

    $scope.$parent.onOk = function () {
        $scope.nv.nGAYSINH = $scope.value;
        AppDialogService.closeDialog({ ok: true, obj: $scope.nv });
    };

    $scope.onCancel = function () {
        AppDialogService.closeDialog({ ok: false });
    };

    $scope.beforeClose = function () {
        return "";
    };

}]);